﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class GameSelect : MonoBehaviour
{
    public Button Button;
    public StartScript StartScript;
    public ScrollRect ScrollRect;
    // Start is called before the first frame update
    void Start()
    {
        //Button = transform.GetChild(0).GetComponent<Button>();
        string MrpName = PlayerPrefs.GetString("JarFileName", "mrp.jar");
        SetButton(Button, MrpName, 1);
        int i = 1;
        string path = StartScript.RootPath;
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        DirectoryInfo root = new DirectoryInfo(path);
        FileInfo[] files = root.GetFiles();
        foreach (var f in files)
        {
            if(f.Extension==".jar")
            {
                Button b = Instantiate(Button, ScrollRect.content);
                SetButton(b, f.Name, f.Length);
                i++;
            }
           
        }
    }

    private void SetButton(Button b,string Name,long Length)
    {
        Text t = b.transform.GetChild(0).GetComponent<Text>();
        //t.text = Name + " " + Length;
        t.text = Name;
        b.onClick.AddListener(() => {
            StartScript.AppName = t.text.Substring(0,t.text.Length-4);
            PlayerPrefs.SetString("JarFileName", Name);
            StartScript.StartGame();
            gameObject.SetActive(false);
        });
    }
}
